package com.ygxsk.carhome.ui.care;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flyco.roundview.RoundTextView;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.listener.ErrorParentExtraSingle;
import com.ygxsk.carhome.listener.RxBus;
import com.ygxsk.carhome.response.CardGroupHeadOrTeamCheck;
import com.ygxsk.carhome.rxbus.RxBusCardGroupHeadMan;
import com.ygxsk.carhome.ui.ErrorActivity;
import com.ygxsk.carhome.ui.adapter.CardGroupHeadManMemberAdapter;
import com.ygxsk.carhome.views.MyLinearLayoutManager;

import java.util.ArrayList;

import butterknife.BindView;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-07-25  19:27
 * @Description:这个是客户信息的列表，是【搜索】完毕之后查询客户信息的
 */
public class CustomerPersonActivity extends ErrorActivity implements ErrorParentExtraSingle {

    @BindView(R.id.refreshlayoutingroupcard)
    SmartRefreshLayout refreshLayoutInGroupCard;

    @BindView(R.id.recyclerviewingroupheadmancard)
    RecyclerView recyclerViewInGroupHeadmanCard;

    //点击确定的按钮
    @BindView(R.id.roundbuttonsuregroupcard)
    RoundTextView roundButtonSureGroupCard;

    private CardGroupHeadManMemberAdapter runManGroupHeadManMemberAdapter;

    private ArrayList<CardGroupHeadOrTeamCheck> mDatasResources = new ArrayList<>();

    //从哪里过来
    private int extra_jumpfrom_where;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        processExtraData();

        initloading();

        initViews();

        // TODO: 2023-07-14 14:47 真实开发放开


//        gainDatas();


    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_customer_person;
    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        toolbarHelper.setTitle(getString(R.string.choose_sell_person));
        toolbarHelper.setVersionName(true, AppUtils.getAppVersionName());
        toolbarHelper.setLayoutClose(false, "", null);
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                methodBack();
            }
        });

        toolbarHelper.setLayoutRight(false, 0, "", null);

    }

    @Override
    protected void onClickNodata(View v) {

        gainDatas();

    }

    @Override
    protected void initViews() {

        //填充view
        //填充假数据布局
        // TODO: 2023-07-14 14:38 填充假数据
        CardGroupHeadOrTeamCheck cardGroupHeadOrTeamCheck = new CardGroupHeadOrTeamCheck();
        cardGroupHeadOrTeamCheck.setGroupUserName("陈大");
        cardGroupHeadOrTeamCheck.setBusinessDeptName("销售顾问");

        CardGroupHeadOrTeamCheck cardGroupHeadOrTeamCheck1 = new CardGroupHeadOrTeamCheck();
        cardGroupHeadOrTeamCheck1.setGroupUserName("陈二");
        cardGroupHeadOrTeamCheck1.setBusinessDeptName("销售顾问");

        CardGroupHeadOrTeamCheck cardGroupHeadOrTeamCheck2 = new CardGroupHeadOrTeamCheck();
        cardGroupHeadOrTeamCheck2.setGroupUserName("陈三");
        cardGroupHeadOrTeamCheck2.setBusinessDeptName("销售顾问");

        mDatasResources.add(cardGroupHeadOrTeamCheck);
        mDatasResources.add(cardGroupHeadOrTeamCheck1);
        mDatasResources.add(cardGroupHeadOrTeamCheck2);


        runManGroupHeadManMemberAdapter = new CardGroupHeadManMemberAdapter(R.layout.groupmemberheadmanadapter, mDatasResources);
        recyclerViewInGroupHeadmanCard.setLayoutManager(new MyLinearLayoutManager(this));
        recyclerViewInGroupHeadmanCard.clearAnimation();
        recyclerViewInGroupHeadmanCard.setAdapter(runManGroupHeadManMemberAdapter);
        runManGroupHeadManMemberAdapter.notifyDataSetChanged();

        runManGroupHeadManMemberAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                CardGroupHeadOrTeamCheck cardGroupHeadOrTeamCheck = (CardGroupHeadOrTeamCheck) adapter.getItem(position);

                CheckBox checkBoxInHeadMan = (CheckBox) view.findViewById(R.id.checkboxinheadman);

//                LogUtils.d(ConstantSign.LOGCAT_NOMAL + "点击了条目内容" + runManGroupHeadOrTeamCheck.getGroupUserName());

                if (cardGroupHeadOrTeamCheck != null) {

                    int isChecked = cardGroupHeadOrTeamCheck.getIsChecked();

                    if (isChecked == 0) {

                        for (int index = 0; index < mDatasResources.size(); index++) {

                            if (index == position) {
                                mDatasResources.get(index).setIsChecked(1);
                                checkBoxInHeadMan.setChecked(true);
                            } else {
                                mDatasResources.get(index).setIsChecked(0);
                                checkBoxInHeadMan.setChecked(false);
                            }

                        }

                    } else if (isChecked == 1) {//如果当前状态是已经被选中的状态

                        //全部置空
                        for (int ind = 0; ind < mDatasResources.size(); ind++) {
                            mDatasResources.get(ind).setIsChecked(0);
                            checkBoxInHeadMan.setChecked(false);
                        }

                    }

                    if (runManGroupHeadManMemberAdapter != null) {
                        runManGroupHeadManMemberAdapter.notifyDataSetChanged();
                    }

                }

            }
        });


        //暂时设置 仅仅只有下拉刷新，没有加载更多；
        refreshLayoutInGroupCard.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                refreshLayout.getLayout().post(new Runnable() {
                    @Override
                    public void run() {

                        if (mDatasResources != null && !mDatasResources.isEmpty()) {
                            mDatasResources.clear();
                        }

                        if (runManGroupHeadManMemberAdapter != null) {
                            runManGroupHeadManMemberAdapter.notifyDataSetChanged();
                        }

                        gainDatas();

                        refreshLayout.finishRefresh();

                    }
                });

            }
        });

        //点击【确定】勾选完毕 小组长
        roundButtonSureGroupCard.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                RxBusCardGroupHeadMan rxBusCardGroupHeadMan = new RxBusCardGroupHeadMan(ExtraValue.RXBUS_RUNMANGROUP_HEADMAN);

                for (CardGroupHeadOrTeamCheck cardGroupHeadOrTeamCheck : mDatasResources) {
                    if (cardGroupHeadOrTeamCheck.getIsChecked() == 1) {
                        rxBusCardGroupHeadMan.setCardGroupHeadOrTeamCheck(cardGroupHeadOrTeamCheck);
//                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + cardGroupHeadOrTeamCheck.getGroupUserName());
                    }
                }

                //拦截
                if (ObjectUtils.isEmpty(rxBusCardGroupHeadMan.getCardGroupHeadOrTeamCheck())) {
                    ToastUtils.showShort(R.string.please_choose_customers);
                    return;
                }

                RxBus.getInstance().post(rxBusCardGroupHeadMan);

                //关闭该页
                ActivityUtils.finishActivity(mActivityInstance);

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

//        if (!TextUtils.isEmpty(extra_runmangroup_headman_teamleaderid)) {
//            extra_runmangroup_headman_teamleaderid = "";
//        }

        if (mDatasResources != null && !mDatasResources.isEmpty()) {
            mDatasResources.clear();
            if (runManGroupHeadManMemberAdapter != null) {
                runManGroupHeadManMemberAdapter.notifyDataSetChanged();
                runManGroupHeadManMemberAdapter = null;
            }
            mDatasResources = null;
        }

    }

    @Override
    protected void gainDatas() {

    }

    @Override
    protected void methodBack() {

        if (ActivityUtils.isActivityAlive(this)) {
            ActivityUtils.finishActivity(this);
        }

    }

    @Override
    public void processExtraData() {

        //

    }
}